package com.mokairui.basic.daemon;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

/**
 * @author Mokairui
 * @Description 守护线程:主线程结束了，守护线程不管有没有结束都会强制结束
 * @Since 2021/6/6
 */
@Slf4j(topic = "c.Daemon")
public class DaemonDemo {
    @SneakyThrows
    public static void main(String[] args) {
        Thread thread = new Thread(() -> {
            while (true) {
                if (Thread.currentThread().isInterrupted()) {
                    break;
                }
            }
            log.debug("线程结束..."); // 不会打印，因为在上面循环的某一次中就被结束掉了
        }, "t1");
        thread.setDaemon(true);
        thread.start();

        TimeUnit.SECONDS.sleep(1);
        log.debug("结束...");
    }
}
